package com.jzr.open.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.open.common.SecurityHelper;
import com.jzr.open.db1.dto.PatientCareLabelDto;
import com.jzr.open.db1.mapper.MyPatientCareLabelMapper;
import com.jzr.open.db1.mapper.TCareLabelMapper;
import com.jzr.open.db1.mapper.TPatientMapper;
import com.jzr.open.db1.model.TCareLabel;
import com.jzr.open.db1.model.TPatient;
import com.jzr.open.db1.model.TPatientCareLabelRef;
import com.jzr.open.db1.service.PatientCareLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PatientCareLabelServiceImpl extends ServiceImpl<MyPatientCareLabelMapper, TPatientCareLabelRef> implements PatientCareLabelService {
    @Autowired
    private TCareLabelMapper careLabelMapper;

    @Autowired
    private TPatientMapper patientMapper;

    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }

    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }

    @Override
    public int save(List<PatientCareLabelDto> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        Integer userId = SecurityHelper.getUserId();
        int count = 0;
        for(PatientCareLabelDto record : list){
            setId(record);
            QueryWrapper<TPatientCareLabelRef> qw = new QueryWrapper<>();
            qw.eq(TPatientCareLabelRef.PatientId_,record.getPatientId())
                    .eq(TPatientCareLabelRef.CareLabelId_,record.getCareLabelId());

            if(record.getIsDel() != null && record.getIsDel().booleanValue() == true){
                count += baseMapper.delete(qw);
                continue;
            }
            setId(record);
            record.setUpdateBy(userId);
            record.setUpdateTime(LocalDateTime.now());
            Integer id = getId(qw);
            if(id == null){
                count += baseMapper.insert(record);
            }else{
                count += baseMapper.update(record,qw);
            }
        }
        return count;
    }

    @Override
    public List<PatientCareLabelDto> get(PatientCareLabelDto record) {
        return baseMapper.get(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(List<PatientCareLabelDto> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        int count = 0;
        for(PatientCareLabelDto record : list){
            setId(record);
            QueryWrapper<TPatientCareLabelRef> qw = new QueryWrapper<>();
            qw.eq(TPatientCareLabelRef.PatientId_,record.getPatientId())
                    .eq(TPatientCareLabelRef.CareLabelId_,record.getCareLabelId());
            count += baseMapper.delete(qw);
        }
        return count;
    }

    private void setId(PatientCareLabelDto record){
        List<Integer> list = patientMapper.getIdList(new QueryWrapper<TPatient>()
                .eq(TPatient.Code_,record.getPatientCode())
                .eq(TPatient.CureNo_,record.getCureNo())
        );
        record.setPatientId(list==null || list.size()<=0 ? null : list.get(0));

        list = careLabelMapper.getIdList(new QueryWrapper<TCareLabel>()
                .eq(TCareLabel.LabelCode_,record.getLabelCode())
        );
        record.setCareLabelId(list==null || list.size()<=0 ? null : list.get(0));
    }
}
